﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
public partial class ExpertManager_AssignTask2 : System.Web.UI.Page
{
    static int CSROW = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Txtime.Enabled = false;
        Txtime.Text = DateTime.Now.ToString();


        string state = DrpTask.SelectedValue;
        string str1 = "SELECT [Name], [Family], [Telephone], [FollowCode], [Type],[Flag], [Address], [Mobile], [ID] FROM [CustCustFolder] WHERE (([ProvinceId] = @ProvinceId) AND ([Flag] = @Flag ) AND [IsDone]= 0 AND ExpertId IS NULL )";
        string str2 = "SELECT [Name], [Family], [Telephone], [FollowCode], [Type],[Flag], [Address], [Mobile], [ID] FROM [CustCustFolder] WHERE (([ProvinceId] = @ProvinceId) AND ([Flag] = @Flag ) AND [IsDone]= 0 AND ExpertId IS NOT NULL )";
        if (state == "false") 
            SqlDataSource1.SelectCommand = str1;
        else
            SqlDataSource1.SelectCommand = str2;



    }
    protected void BTnSEndTask_Click(object sender, EventArgs e)
    {
        List<int> CustomerID = new List<int>();
       
           
        for (int count = 0; count < GridView1.Rows.Count; count++)
        {
            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //if (row.RowType == DataControlRowType.DataRow)
            //{

            // instead row put GridView1.Rows[count]
            CheckBox chkRow = (GridView1.Rows[count].Cells[8].FindControl("chkCtrl") as CheckBox);
            if (chkRow.Checked)
            {
                if (Convert.ToBoolean(GridView1.Rows[count].Cells[8].Text) == true)
                {

                    IDataReader datar = TblCusFolder.FetchByParameter(TblCusFolder.Columns.Flag, SubSonic.Comparison.Equals, 1);
                    while (datar.Read())
                    {
                        if (Convert.ToBoolean(datar["IsDone"]) == false)
                        {
                            TblCusFolder cust = new TblCusFolder();
                            cust.RecordSave = Convert.ToInt32(datar["ID"]);
                            cust.ExpDescription = datar["ExpDescription"].ToString();
                            cust.Type = datar["Type"].ToString();
                            cust.ExpertId = Convert.ToInt32(datar["ExpertId"]);
                            cust.RegDate = Convert.ToDateTime(datar["RegDate"]);
                            cust.CusId = Convert.ToInt32(datar["CusId"]);
                            cust.IsDone = false;
                            cust.Flag = false;
                            cust.Save();
                        }
                    }
                    datar.Close();

                }
                CustomerID.Add(Convert.ToInt32(GridView1.Rows[count].Cells[7].Text));
            }
            foreach (int Id in CustomerID)
            {
                SubSonic.Query Qr = TblCusFolder.CreateQuery();
                Qr.QueryType = SubSonic.QueryType.Update;
                Qr.AddUpdateSetting(TblCusFolder.Columns.ExpertId, DrpExpert.SelectedValue);
                Qr.AddUpdateSetting(TblCusFolder.Columns.RegDate, Txtime.Text);
                Qr.AddUpdateSetting(TblCusFolder.Columns.Flag, 0);
                Qr.WHERE(TblCusFolder.Columns.Id, SubSonic.Comparison.Equals, Id);

                Qr.Execute();

            }
            GridView1.DataBind();
        }
    }
    protected void chkCtrl_CheckedChanged(object sender, EventArgs e)
    {


        
        CheckBox ch = sender as CheckBox;
        if (ch.Checked)
            CSROW++;
        else
            CSROW--;


        if (CSROW == 0)
        
            BTnSEndTask.Enabled = false;
            
        else
            BTnSEndTask.Enabled = true;

    }
    protected void DrpTask_SelectedIndexChanged(object sender, EventArgs e)
    {
        CSROW = 0;
        BTnSEndTask.Enabled = false;
    }
    protected void drProvinces_SelectedIndexChanged(object sender, EventArgs e)
    {
        CSROW = 0;
        BTnSEndTask.Enabled = false;
    }
} 
